/*
 * Created on Apr 2, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.control.net;

import java.lang.Thread;
import littlecyberwar.util.*;
import littlecyberwar.ui.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ClientThread extends Thread {
	public static final String _WAIT="Connecting to Server...";
	public static final String _CONNECTED="connected";
	public static final String _DISCONNECTED="Disconnected";
	
	MainFrame mf;
	int port;
	InetAddress ip;
	

	public ClientThread(JFrame parent, InetAddress ipIn, int portNumIn) {
		ip = ipIn;
		port =portNumIn;
		Thread currThread = Thread.currentThread();
		int priority = currThread.getPriority();
		// Debug.out("Calling parent thread priority= "+ priority);
		this.setPriority(priority-1);
		mf = (MainFrame)parent;
	}
	
	public void run() {
		Debug.out("ClientThread Started!");
		mf.setOnlineStatus(_WAIT);
		Socket socket=null;
		
		try {
			socket = new Socket(ip, port);
		} catch ( IOException ex ) {
			JOptionPane.showMessageDialog(mf,
				"IO Error Encountered while\n"+
				"Attempting to Connect to Server...",
				"IO Error Encountered",
				JOptionPane.ERROR_MESSAGE);
					
			mf.setOnlineStatus(_DISCONNECTED);		
			this.stop();			
		}
		// Debug.out("Socket created!");	
		mf.setOnlineStatus(_CONNECTED);	
		NetHandler.socketCreated(socket);
		return;
	}
}
